#ifndef GB28181_MESSAGE_H
#define GB28181_MESSAGE_H

#include <string>
#include <functional>

extern "C" {
#include <eXosip2/eXosip.h>
#include <osipparser2/osip_message.h>
}

namespace El {
namespace GB28181 {

// Forward declarations
struct SipServerConfig;
struct PTZControlInfo;
struct RecordQueryInfo;
struct RecordInfo;

// Message handler for GB28181 protocol
class GB28181Message {
public:
    // Message types
    enum class MessageType {
        UNKNOWN,
        DEVICE_INFO,
        CATALOG,
        KEEPALIVE,
        DEVICE_STATUS,
        RECORD_INFO,
        ALARM,
        CONFIG_DOWNLOAD,
        PRESET_QUERY,
        DEVICE_CONTROL
    };
    
    // Process incoming MESSAGE request
    static void ProcessMessageRequest(eXosip_event_t *event, eXosip_t *exosip_context,
                                    const SipServerConfig &config,
                                    std::function<void(const std::string&)> responseCallback,
                                    std::function<bool(const PTZControlInfo&)> ptzCallback = nullptr,
                                    std::function<std::vector<RecordInfo>(const RecordQueryInfo&)> recordCallback = nullptr);
    
    // Send heartbeat/keepalive message
    static void SendHeartbeat(eXosip_t *exosip_context, const SipServerConfig &config);
    
    // Send response to a MESSAGE request
    static void SendResponse(eXosip_t *exosip_context, eXosip_event_t *event, 
                           int status_code, const std::string &content = "");
    
    // Create heartbeat XML
    static std::string CreateHeartbeatXml(const std::string &device_id, const std::string &sn);
    
    // Create alarm notification XML
    static std::string CreateAlarmXml(const std::string &device_id, const std::string &sn,
                                    int alarm_priority, int alarm_method,
                                    const std::string &alarm_time,
                                    const std::string &alarm_description);
    
    // Parse message type from XML content
    static MessageType ParseMessageType(const std::string &xml);
    
    // Check if content type is MANSCDP+xml
    static bool IsMANSCDPMessage(osip_message_t *message);
    
    // Extract XML body from SIP message
    static std::string ExtractXmlBody(osip_message_t *message);
    
    // Build MESSAGE request
    static osip_message_t* BuildMessageRequest(eXosip_t *exosip_context,
                                              const std::string &from,
                                              const std::string &to,
                                              const std::string &xml_content);
};

} // namespace GB28181
} // namespace El

#endif // GB28181_MESSAGE_H